package com.th.supcom.pds.sortingdrug.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.carefx.framework.container.client.exception.UiException;
import net.carefx.framework.util.ConvertUtils;
import net.carefx.framework.web.gwt.server.GwtController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.pds.common.enums.SortingIdentification;
import com.th.supcom.pds.domain.pojo.PdsSupplyPro;
import com.th.supcom.pds.internal.api.PdsSupplyProService;
import com.th.supcom.pds.sortingdrug.client.model.PdsSupplyProModel;
import com.th.supcom.pds.sortingdrug.client.service.SortingDrugRpc;

/**
 * SortDrug 控制层
 * 
 * @author 杨冲
 * 
 */
public class SortingDrugController extends GwtController implements SortingDrugRpc
{

    private static final long serialVersionUID = -3168971886748123892L;
    private PdsSupplyProService m_pdsSupplyProService;
    /**
     * 日志记录器.
     */
    public static final Logger s_logger = LoggerFactory.getLogger (SortingDrugController.class);

    public void setPdsSupplyProService (PdsSupplyProService pdsSupplyProService)
    {

        m_pdsSupplyProService = pdsSupplyProService;
    }

    public SortingDrugController ()
    {
    }

    @Override
    public Map <Long, List <PdsSupplyProModel>> findPrescDetailInfoByDept (String deptCode) throws UiException
    {
        s_logger.info ("调用findPrescDetailInfoByDept找出已收方收费未分拣的处方，入参：deptCode:{}", new Object[]
        { deptCode });
        List <PdsSupplyPro> list = null;
        PdsSupplyPro pro = new PdsSupplyPro ();
        pro.setSortingIdentification (SortingIdentification.UnSorting.getEnumCode ());
        pro.setPrescriptionBy (deptCode);
        try
        {
            list = m_pdsSupplyProService.findPdsSupplyProByExample (pro);
        }
        catch (final Exception e)
        {
            s_logger.error ("查询未分拣信息异常,异常信息:", new Object[]
            { e });
            throw new UiException ("查询处方信息失败", e);
        }

        List <PdsSupplyProModel> resultList = ConvertUtils.convert (PdsSupplyProModel.class, list);

        Map <Long, List <PdsSupplyProModel>> map = new HashMap <Long, List <PdsSupplyProModel>> ();

        for (PdsSupplyProModel model : resultList)
        {
            List <PdsSupplyProModel> models = null;
            if (!map.containsKey (Long.parseLong (model.getOutpDispensaryWin ())))
            {
                models = new ArrayList <PdsSupplyProModel> ();
            }
            else
            {
                models = map.get (Long.parseLong (model.getOutpDispensaryWin ()));
            }
            models.add (model);
            map.put (Long.parseLong (model.getOutpDispensaryWin ()), models);
        }

        return map;
    }

    @Override
    public void modifyPrescDetailByList (List <PdsSupplyProModel> models) throws UiException
    {
        s_logger.info ("调用modifyPrescDetailByList修改处方过信息的分拣状态,参数信息：models:", new Object[]
        { models });
        if (models.isEmpty ())
        {
            return;
        }

        List <PdsSupplyPro> pdsSupplyPros = ConvertUtils.convert (PdsSupplyPro.class, models);
        try
        {
            m_pdsSupplyProService.modifyPdsSupplyProSortInfoByList (pdsSupplyPros);
        }
        catch (final Exception e)
        {
            s_logger.error ("修改处方过信息的分拣状态异常！异常信息：", new Object[]
            { e });
            throw new UiException ("更新处方分拣信息失败", e);
        }
    }
}
